package com.yilin.tms.core.commons.zysdk;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class SecurityUtil {
    public static final String HMAC_SHA1 = "HmacSHA1";

    public static byte[] hmacSha1(byte[] data, byte[] key, int offset, int len) {
        SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");
        Mac mac = null;

        try {
            mac = Mac.getInstance("HmacSHA1");
            mac.init(signingKey);
        } catch (NoSuchAlgorithmException var7) {
            throw new RuntimeException(var7.getMessage(), var7);
        } catch (InvalidKeyException var8) {
            throw new RuntimeException(var8.getMessage(), var8);
        }

        mac.update(data, offset, len);
        return mac.doFinal();
    }

    public static byte[] hmacSha1(byte[][] datas, byte[] key) {
        SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");
        Mac mac = null;

        try {
            mac = Mac.getInstance("HmacSHA1");
            mac.init(signingKey);
        } catch (NoSuchAlgorithmException var8) {
            throw new RuntimeException(var8.getMessage(), var8);
        } catch (InvalidKeyException var9) {
            throw new RuntimeException(var9.getMessage(), var9);
        }

        byte[][] var7 = datas;
        int var6 = datas.length;

        for (int var5 = 0; var5 < var6; ++var5) {
            byte[] data = var7[var5];
            mac.update(data);
        }

        return mac.doFinal();
    }

    public static byte[] hmacSha1(String[] datas, byte[] key) {
        SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");
        Mac mac = null;

        try {
            mac = Mac.getInstance("HmacSHA1");
            mac.init(signingKey);
        } catch (NoSuchAlgorithmException var8) {
            throw new RuntimeException(var8.getMessage(), var8);
        } catch (InvalidKeyException var9) {
            throw new RuntimeException(var9.getMessage(), var9);
        }

        try {
            String[] var7 = datas;
            int var6 = datas.length;

            for (int var5 = 0; var5 < var6; ++var5) {
                String data = var7[var5];
                mac.update(data.getBytes("UTF-8"));
            }
        } catch (UnsupportedEncodingException var10) {
            throw new RuntimeException(var10.getMessage(), var10);
        }

        return mac.doFinal();
    }

    public static String hmacSha1ToHexStr(byte[] data, byte[] key, int offset, int len) {
        byte[] rawHmac = hmacSha1(data, key, offset, len);
        return SignStringUtil.encodeHexStr(rawHmac);
    }

    public static String hmacSha1ToHexStr(byte[] data, String key, int offset, int len) {
        try {
            return hmacSha1ToHexStr(data, key.getBytes("UTF-8"), offset, len);
        } catch (UnsupportedEncodingException var5) {
            throw new RuntimeException(var5.getMessage(), var5);
        }
    }

    public static String hmacSha1ToHexStr(String str, String key) {
        try {
            byte[] data = str.getBytes("UTF-8");
            return hmacSha1ToHexStr(data, (byte[]) key.getBytes("UTF-8"), 0, data.length);
        } catch (UnsupportedEncodingException var3) {
            throw new RuntimeException(var3.getMessage(), var3);
        }
    }

    private SecurityUtil() {
    }
}
